package com.qst.controller;

import com.qst.*;
import com.qst.entity.Team;
import com.qst.entity.TestPersonnel;
import com.qst.entity.User;
import com.qst.mapper.UserMapper;
import com.qst.service.TeamService;
import com.qst.service.TestPersonnelService;
import com.qst.service.UserAdminService;
import com.qst.service.impl.ServiceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController extends BaseServlet {

    @Autowired
    @Qualifier("userAdminServiceSsmImpl")
    private  UserAdminService userAdminService;

    @Autowired
    @Qualifier("testPersonnelSsmServiceImpl")
    private TestPersonnelService testPersonnelService;

    @Autowired
    private  UserMapper userMapper;

    private TeamService classTeamService = ServiceFactory
            .getService(TeamService.class);

    //列表
    @RequestMapping("/list.action")
    public String list(Model model) {
        List<User> users = userAdminService.findUsers();
        model.addAttribute("userList", users);
        return "user/list";
    }

    //删除
    @RequestMapping("/delete.action")
    public String delete(String id) {
        //获取前台传递的ID
        int i = Integer.parseInt(id);
        //删除id对应的用户
        userAdminService.deleteById(i);
        //重定向跳转到另一个URL
        return "redirect:/user/list.action";
    }

    //查看页面详情
    @RequestMapping("/view.action")
    public String view(String id, Model module) {
        if (id == null && "".equals(id)) {
            new ExamException("入参错误!");
        }
        //拿到前台传递的参数ID
        int i = Integer.parseInt(id);
        //通过ID获取用户详情
        User user = userAdminService.findById(i);
        //设置页面参数
        module.addAttribute("user", user);
        //跳转页面
        return "user/view";
    }

    //跳转新增页面
    @RequestMapping("/create.action")
    public String create(User user, Model model) {
        //赋默认值
        user.setStatus(1);
        user.setType(1);
        model.addAttribute("user", user);
        return "user/create";
    }

    @RequestMapping("/save.action")
    public String save(User user, Model model) {
        //保存用户
        userAdminService.saveUser(user);
        return "forward:/user/list.action";
    }

    @RequestMapping("/edit.action")
    public String edit(String id, Model module) {
        //拿到前台传来的参数ID
        int i = Integer.parseInt(id);
        //通过id获取用户
        User user = userAdminService.findById(i);
        //设置页面参数，跳转详情页
        module.addAttribute("user", user);
        return "user/edit";
    }

    @RequestMapping("/update.action")
    public String update(User user, Model model) {
        //更新用户
        userAdminService.updateUser(user);
        return "redirect:/user/list.action";
    }

    //重置密码
    @RequestMapping("/password.action")
    public String password(String id) {
        //拿到前台传来的参数ID
        int i = Integer.parseInt(id);
        //重置对应ID的密码为初始化密码
        userAdminService.resetPassword(i);
        //重定向到列表页
        return "redirect:/user/list.action";
    }

    //跳转注册页面
    @RequestMapping("/showReg.do")
    public String showReg(Model model) {
        List<Team> teamList = classTeamService.findAll();
        model.addAttribute("teamList", teamList);
        return "forward:/user/reg.jsp";
    }
    @RequestMapping("/reg.do")
    public String reg(HttpServletRequest req) {
        User user = new User();
        req.getParameter("");
        user.setId(RequestUtil.getInt(req, "id"));
        user.setLogin(RequestUtil.getString(req, "login"));
        user.setPasswd(RequestUtil.getString(req, "passwd"));
        user.setName(RequestUtil.getString(req, "name"));
        user.setType(RequestUtil.getInt(req, "type"));
        user.setStatus(RequestUtil.getInt(req, "status"));
        TestPersonnel s = new TestPersonnel();
        s.setUser(user);
        s.setGender(RequestUtil.getString(req, "gender"));
        s.setPhone(RequestUtil.getString(req, "login"));
        s.setTeamId(RequestUtil.getInt(req, "teamId"));
        try {
            if(userMapper.findByLogin(s.getPhone())!=null){
                addMessage(req, "该手机号已被注册！");
                return "redirect:/user/showReg.do";
            }else{
                testPersonnelService.addTestPersonnel(s);
                addMessage(req, "用户信息已保存");
                req.getSession().setAttribute(Constant.CURRENT_USER, user);
                req.getSession().setAttribute(Constant.CURRENT_TESTPERSONNEL, s);
                return "redirect:/login.jsp";
              }
        } catch (ExamException ex) {
            req.setAttribute("user", user);
            addError(req, ex.getMessage());
            return "forward:/user/create.jsp";
        }
    }

}
